// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

// game art from : https://openpixelproject.itch.io/opp2017sprites

cc.macro.ENABLE_TILEDMAP_CULLING = false;

cc.Class({
    extends: cc.Component,

    properties: {
        maxSpeed: 500,
        jumps: 2,
        acceleration: 1500,
        jumpSpeed: 200,
        drag: 600,
        locatePoint:{
            type:cc.Sprite,
            default: null,
            tooltip:'子光'

        },
        actorAtlas:{
            type:cc.SpriteAtlas,
            default: null,
            tooltip:'角色图集资源'
        },
        smallStar:{
            type:cc.Sprite,
            default: null,
            tooltip:'小星星'
        },
        bigStar:{
            type:cc.Sprite,
            default: null,
            tooltip:'大星星'
        },
        eye:{
            type:cc.Sprite,
            default: null,
            tooltip:'眼睛节点'
        },
        particles: {
            default: [],
            type: [cc.Node],
            tooltip:'粒子'
        },

    },

    // use this for initialization
    onLoad: function () {
        this.debug = true;
        this._left = false;
        this._right = false;
        this._up = false;
        this._space = false;
        // 可操作
        this.canOperate = false;
        this.body = this.getComponent(cc.RigidBody);
        // 处于哪个位面 0：光明；1：黑暗
        this.plane = 0; 
        // 位面切换的CD
        this.planeSwitchCD = 0;
        // 另一位面碰撞箱碰撞
        this.onStoneCount = 0;  
        // 退出双击有效计时
        this.doubleBackTime = 0;
        // 节点动画组件
        this.anim = this.getComponent(cc.Animation);
    },

    //与其它碰撞箱开始碰撞时：死亡/确认无法切换位面
    onBeginContact: function (contact, selfCollider, otherCollider) {
        if (otherCollider.tag === 6) {
            //过关
            this.manager.levelPass();
        } else if (this.manager.passCondition && otherCollider.tag === this.manager.passCondition) {
            //事件PASS条件对应碰撞标签碰撞体被碰时，确认PASS
            this.manager.onPass = true;
        } else if (otherCollider.tag === 4) {
            //死亡
            this.manager.gameOver();
        } else if (otherCollider.tag === 2) {
            //确认没法切换
            this.onStoneCount +=1;
        } else if (otherCollider.tag === 0 && contact.getManifold().localNormal.y <= -0.4) {
            // 在地面时恢复二段跳(碰到地面、跳跃计数小于2、脚底碰触)
            this.jumps = 2;
            this.locatePoint.node.active = true;
        } 
    },

    //与其它碰撞箱取消碰撞时：取消无法切换位面
    onEndContact: function (contact, selfCollider, otherCollider) {
        if (this.passCondition && otherCollider.tag === this.passCondition) {
            //事件PASS条件对应碰撞标签碰撞体被碰时，取消PASS
            this.onPass = false;
        } else if (otherCollider.tag === 2) {
            //取消掉没法切换的计数
            this.onStoneCount -=1;
        }
    },

    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        var speed = (Math.abs(this.body.linearVelocity.x) > 500) ? 620 : Math.abs(this.body.linearVelocity.x) + 120;
        this.smallStar.node.angle -= speed * dt;
        this.bigStar.node.angle += speed * dt;
    },

    // 切换位面操作
    canSwitchPlane () {
        // 没有与另一位面监视碰撞箱的情况
        if (this.onStoneCount === 0) {
            return true;
        };
        
        return false;
    },
    
    // 切换到光明位面
    switchLightPlane () {
        // 主角图像切换
        this.bigStar.spriteFrame = this.actorAtlas.getSpriteFrame("TriAngle_Light");
        this.smallStar.spriteFrame = this.actorAtlas.getSpriteFrame("TriAngle_Light");
        this.eye.spriteFrame = this.actorAtlas.getSpriteFrame("Eye_Light");
        this.locatePoint.spriteFrame = this.actorAtlas.getSpriteFrame("Shadow_Light");
        this.particles[0].active = true;
        this.particles[1].active = false;
    },
    
    // 切换到暗影位面
    switchDarkPlane () {
        // 主角图像切换
        this.bigStar.spriteFrame = this.actorAtlas.getSpriteFrame("TriAngle_Dark");
        this.smallStar.spriteFrame = this.actorAtlas.getSpriteFrame("TriAngle_Dark");
        this.eye.spriteFrame = this.actorAtlas.getSpriteFrame("Eye_Dark");
        this.locatePoint.spriteFrame = this.actorAtlas.getSpriteFrame("Shadow_Dark");
        this.particles[1].active = true;
        this.particles[0].active = false;
    },

    // 结束死亡
    die () {
        // 主角图像切换
        this.particles[0].active = false;
        this.particles[1].active = false;
        this.particles[2].active = false;
        this.body.linearVelocity = new cc.Vec2(0, 0);
        this.body.gravityScale = 0;
        console.log([this.body.linearVelocity.x, this.body.linearVelocity.y]);
    },
    
    switchDirection () {
        this.particles[2].scaleX *= -1;
    },
    // 跳跃操作
    jump() {
        if (this.jumps > 0) {
            var speed = this.body.linearVelocity;
            if (speed.y < this.jumpSpeed) speed.y = this.jumpSpeed;
            this.body.linearVelocity = speed;
            this.jumps--;
            this.locatePoint.node.active = false;
            AudioManager.playSe('跳跃', 0.9);
        }
    },
});
